package com.hmkj.core.api.juhe.sdk;

import com.alibaba.fastjson.JSON;
import com.hmkj.common.utils.StringUtils;
import com.hmkj.core.api.juhe.bean.epay.EPayConsts;
import com.hmkj.core.api.juhe.bean.epay.PayModel;
import com.hmkj.core.api.juhe.bean.epay.RealNameAuthResp;
import com.hmkj.core.api.juhe.bean.epay.WithHoldingResp;
import com.hmkj.core.api.juhe.bean.epay.base.DateTimeHelper;
import com.hmkj.core.api.juhe.bean.epay.endpoints.PaymentEndPoint;
import org.apache.http.NameValuePair;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 描述: 双乾支付相关回掉接口
 * 作者: syx
 * 时间: 2017/3/14 17:05
 */
@Component
public class EPayApi {

    /**
     * 双钱认证支付接口
     *
     * @param model
     * @param notifyUrl
     * @param web_url
     * @return
     */
    public WithHoldingResp hfDaikouTrade(PayModel model, String notifyUrl, String web_url) throws Exception {
        if (StringUtils.isBlank(model.getPhone()) || StringUtils.isBlank(model.getBankCode()) || StringUtils.isBlank(model.getBankNo())) {
            throw new Exception("手机号码、银行卡或银行编码不能为空");
        }
        if (StringUtils.isBlank(model.getAccountName()) || StringUtils.isBlank(model.getIdCardNo())) {
            throw new Exception("真实姓名、身份证号不能为空");
        }
        if (StringUtils.isBlank(model.getAmount()) || "0".equals(model.getAmount())) {
            throw new Exception("金额不能为空且不能为0");
        }
        if (StringUtils.isBlank(model.getSmsCode())) {
            throw new Exception("验证码不能为空");
        }
        if (StringUtils.isBlank(model.getMerOrderNo())) {
            throw new Exception("商户订单号不能为空");
        }
        List<NameValuePair> formparams;
        if (EPayConsts.OBLINE_OPEN) {
            formparams = PaymentEndPoint.buildQuickNameParams(model, notifyUrl, EPayConsts.MERC_NO, EPayConsts.MD5KEY, web_url);
        } else {
            formparams = PaymentEndPoint.buildQuickNameParams(model, notifyUrl, EPayConsts.MERC_NO_TEST, EPayConsts.MD5KEY_TEST, web_url);
        }

        WithHoldingResp res = PaymentEndPoint.hfDaiKouTrade(formparams);

        //保存接口调用记录
        try {
            res.setRequestParams(JSON.toJSONString(formparams));
            res.setResponseParams(JSON.toJSONString(res));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 双乾认证支付获取验证码
     *
     * @param model
     * @return
     * @throws Exception
     */
    public RealNameAuthResp realnameAuth(PayModel model) throws Exception {
        if (StringUtils.isBlank(model.getPhone()) || StringUtils.isBlank(model.getBankCode()) || StringUtils.isBlank(model.getBankNo())) {
            throw new Exception("手机号码、银行卡或银行编码不能为空");
        }
        if (StringUtils.isBlank(model.getAccountName()) || StringUtils.isBlank(model.getIdCardNo())) {
            throw new Exception("真实姓名、身份证号不能为空");
        }
        if (StringUtils.isBlank(model.getAmount()) || "0".equals(model.getAmount())) {
            throw new Exception("金额不能为空且不能为0");
        }
        if (StringUtils.isBlank(model.getMerOrderNo())) {
            throw new Exception("商户订单号不能为空");
        }
        String transDate = DateTimeHelper.getNowTime("yyyyMMdd");
        String transTime = DateTimeHelper.getNowTime("HHmmss");
        List<NameValuePair> formparams;
        if (EPayConsts.OBLINE_OPEN) {
            formparams = PaymentEndPoint.buildRealNameParams(model, transDate, transTime, EPayConsts.MERC_NO, EPayConsts.MD5KEY);
        } else {
            formparams = PaymentEndPoint.buildRealNameParams(model, transDate, transTime, EPayConsts.MERC_NO_TEST, EPayConsts.MD5KEY_TEST);
        }
        RealNameAuthResp res = PaymentEndPoint.sendCode(formparams);
        try {
            res.setResponseParams(JSON.toJSONString(res));
            res.setRequestParams(JSON.toJSONString(formparams));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return res;
    }
}
